package cn.antho.basic.util;

import cn.antho.basic.config.SpringUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

public class EmailUtil {


    public static Boolean sendEmail(String subject, String text, String sendTo) {
        try {
            ApplicationContext context = SpringUtil.getContext();
            JavaMailSender javaMailSender = context.getBean(JavaMailSender.class);
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setFrom("575786294@qq.com");
            helper.setSubject(subject);
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText(text,true);
            // 5.设置收件人
            helper.setTo(sendTo);
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return false;

    }
}
